package com.example.qxfw.qxzs.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.zy.Zhishu;
import com.example.qxfw.qxzs.mapper.ZhishuMapper;
import com.example.qxfw.qxzs.service.ZhishuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ZhishuServiceImpl implements ZhishuService {

    @Autowired
    private ZhishuMapper zhishuMapper;

    @Override
    public List<String> getCityId(String cityName, String dateChar, String timeChar) {
        List<String> cityId = zhishuMapper.selectCityId(cityName, dateChar, timeChar);
        return cityId;
    }

    @Override
    public List<Zhishu> listByParam(String cityId, String dateChar1, String timeChar1, String dateChar2, String timeChar2) {
        return zhishuMapper.selectList(Wrappers.<Zhishu>query()
                .select("CityID, CityName, to_char(DateChar,'yyyy-mm-dd') as datechar, TimeChar, Longitude, " +
                        "Latitude,  FSGrade|| ':'|| FSText as fsgrade,  MBGrade|| ':' || MBText as mbgrade, " +
                        "ZZGrade|| ':' || ZZText as zzgrade, LSGrade|| ':' ||LSText as lsgrade,  " +
                        "LYGrade||':'|| LYText as lygrade,  RDGrade|| ':' || RDText as rdgrade, SSGrade|| ':' ||SSText as ssgrade, " +
                        "ZWGrade|| ':' || ZWText as zwgrade, PJGrade||':' || PJText as pjgrade, KTGrade|| ':' || KTText as ktgrade, " +
                        "TGGrade|| ':' || TGText as tggrade,  YSGrade|| ':' || YSText as ysgrade,  WRGrade|| ':' || WRText as wrgrade, " +
                        "BYXGrade|| ':' || BYXText as byxgrade,  ZSGrade|| ':' || ZSText as zsgrade, " +
                        " GMGrade|| ':' || GMText as gmgrade, FYGrade|| ':' ||FYText as fygrade,  " +
                        "SHXGrade|| ':' || SHXText as shxgrade, XHXGrade|| ':' || XHXText as xhxgrade, " +
                        "XCGrade|| ':' || XCText as xcgrade, NYXGrade|| ':' ||NYXText as nyxgrade, " +
                        "NGSGrade|| ':' || NGSText as ngsgrade, GXBGrade|| ':' || GXBText as gxbgrade, " +
                        " XJGSGrade|| ':' || XJGSText as xjgsgrade, GXYGrade||':' || GXYText as gxygrade, " +
                        " LYOUGrade|| ':' || LYOUText as lyougrade, CDGrade|| ':' || CDText as cdgrade, " +
                        "JTGrade|| ':' || JTText as jtgrade,  YDGrade|| ':' || YDText as ydgrade, " +
                        "HXGrade|| ':' ||HXText as hxgrade, CLGrade|| ':' ||CLText as clgrade ")
                .lambda()
                .eq(StringUtils.isNotBlank(cityId)&&!cityId.equals("全部"),Zhishu::getCityid,cityId)
                .and(StringUtils.isNotBlank(dateChar2),w->w
                        .and(e->e
                                .eq(Zhishu::getDatechar,Timestamp.valueOf(dateChar1))
                                .ge(Zhishu::getTimechar,timeChar1)
                                .or()
                                .eq(Zhishu::getDatechar,Timestamp.valueOf(dateChar2))
                                .le(Zhishu::getTimechar,timeChar2)
                        )
                        .or()
                        .between(Zhishu::getDatechar,Timestamp.valueOf(dateChar1), Timestamp.valueOf(dateChar2))
                )
                .and(StringUtils.isBlank(dateChar2),w->w
                        .eq(Zhishu::getDatechar,Timestamp.valueOf(dateChar1))
                        .eq(Zhishu::getTimechar,timeChar1)));
    }

    @Override
    public List<Zhishu> getList(String cityId, String dateChar, String timeChar) {
        List<Zhishu> zhishus = zhishuMapper.selectList1(cityId, Timestamp.valueOf(dateChar), timeChar);
        return zhishus;
    }

    @Override
    public Integer saveZhishu(Zhishu zhishu) {
        Integer rows = zhishuMapper.insert(zhishu);
        return rows;
    }

    @Override
    public Integer updateZhishu(Zhishu zhishu) {
        Integer rows = zhishuMapper.updateById(zhishu);
        return rows;
    }

    @Override
    public Integer deleteZhishu(Zhishu zhishu) {
        Integer rows = zhishuMapper.delete(Wrappers.<Zhishu>lambdaQuery()
                .eq(Zhishu::getCityid,zhishu.getCityid())
                .eq(Zhishu::getCityname,zhishu.getCityname())
                .eq(Zhishu::getDatechar,zhishu.getDatechar())
                .eq(Zhishu::getTimechar,zhishu.getTimechar())
                .eq(Zhishu::getLongitude,zhishu.getLongitude())
                .eq(Zhishu::getLatitude,zhishu.getLatitude()));
        return rows;
    }

    @Override
    public List<Zhishu> getMaxTime() {
        List<Zhishu> maxTime = zhishuMapper.selectMaxTime();
        return maxTime;
    }

    @Override
    public List<Zhishu> getList2() {
        List<Zhishu> zhishus = zhishuMapper.selectList2();
        return zhishus;
    }

    @Override
    public List<Zhishu> getList3(String sqlStr) {
        List<Zhishu> zhishus = zhishuMapper.selectList3(sqlStr);
        return zhishus;
    }

    @Override
    public List<Zhishu> getList4(String cityId, String dateChar, String timeChar, String dateChar1, String timeChar1) {
        return zhishuMapper.selectList4(cityId,Timestamp.valueOf(dateChar),Timestamp.valueOf(dateChar1),timeChar,timeChar1);
    }

    @Override
    public List<Zhishu> getList5(String dateChar, String timeChar) {
        return zhishuMapper.selectList5(Timestamp.valueOf(dateChar),timeChar);
    }

    @Override
    public List<Zhishu> getList6(String dateChar, String timeChar, String dateChar1, String timeChar1) {
        return zhishuMapper.selectList6(Timestamp.valueOf(dateChar),Timestamp.valueOf(dateChar1),timeChar,timeChar1);
    }
}
